package com.eastdigit.bpm.util;

import net.sf.json.JSONObject;

import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;

import com.eastdigit.util.DateUtil;
import com.eastdigit.util.StringUtil;

/**
 * Created by Administrator on 2015/9/30.
 */
public class BpmUtil {
    
    
    public static JSONObject toJSONObject(ProcessDefinition processDefinition){
    	JSONObject obj = new JSONObject();
    	if(processDefinition != null){
    		obj.put("id", processDefinition.getId());
        	obj.put("key", processDefinition.getKey());
        	obj.put("name", processDefinition.getName());
        	obj.put("version", processDefinition.getVersion());
        	obj.put("category", processDefinition.getCategory());
        	obj.put("description", processDefinition.getDescription());
        	obj.put("deploymentId", processDefinition.getDeploymentId());
    	}
    	return obj;
    }
    
    public static JSONObject toJSONObject(HistoricProcessInstance historicProcessInstance){
    	JSONObject obj = new JSONObject();
    	if(historicProcessInstance != null){
    		obj.put("processInstanceId", historicProcessInstance.getId());
    		obj.put("businessKey", historicProcessInstance.getBusinessKey());
    		obj.put("name", historicProcessInstance.getName());
    		obj.put("processDefinitionId", historicProcessInstance.getProcessDefinitionId());
    		obj.put("startTime", DateUtil.formatDateTime(historicProcessInstance.getStartTime()));
    		obj.put("endTime", DateUtil.formatDateTime(historicProcessInstance.getEndTime()));
    		obj.put("starter", historicProcessInstance.getStartUserId());
    		obj.put("superProcessInstanceId", historicProcessInstance.getSuperProcessInstanceId());
    	}
    	return obj;
    }
    
    public static JSONObject toJSONObject(Task task){
    	JSONObject obj = new JSONObject();
    	if(task != null){
    		obj.put("id", task.getId());
    		obj.put("owner", StringUtil.noNull(task.getOwner()));
    		obj.put("assignee", StringUtil.noNull(task.getAssignee()));
    		obj.put("name", StringUtil.noNull(task.getName()));
    		obj.put("description", StringUtil.noNull(task.getDescription()));
    		obj.put("createTime", DateUtil.formatDateTime(task.getCreateTime()));
    		obj.put("dueDate", DateUtil.formatDateTime(task.getDueDate()));
    		obj.put("suspended", task.isSuspended());
    		obj.put("category", StringUtil.noNull(task.getCategory()));
    		obj.put("processInstanceId", task.getProcessInstanceId());
    		obj.put("processDefinitionId", StringUtil.noNull(task.getProcessDefinitionId()));
    	}
    	return obj;
    }
    
    public static JSONObject toJSONObject(HistoricTaskInstance task){
    	JSONObject obj = new JSONObject();
    	if(task != null){
    		obj.put("id", task.getId());
    		obj.put("owner", StringUtil.noNull(task.getOwner()));
    		obj.put("assignee", StringUtil.noNull(task.getAssignee()));
    		obj.put("name", StringUtil.noNull(task.getName()));
    		obj.put("description", StringUtil.noNull(task.getDescription()));
    		obj.put("startTime", DateUtil.formatDateTime(task.getStartTime()));
    		obj.put("endTime", DateUtil.formatDateTime(task.getEndTime()));
    		obj.put("dueDate", DateUtil.formatDateTime(task.getDueDate()));
    		obj.put("category", StringUtil.noNull(task.getCategory()));
    		obj.put("processInstanceId", task.getProcessInstanceId());
    		obj.put("processDefinitionId", StringUtil.noNull(task.getProcessDefinitionId()));
    	}
    	return obj;
    }
}
